<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("customerSession.inc.php");
include_once ("Session.php");					//-- Session
include_once ("Password.php");					//-- Password
include_once("Strings.php");					//--> String utils

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

require_once('includeHttps.php');

$common 		= new Common();
$ins_session	= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session
$objPassword 	= new Password();

$objCart 		= new Cart();		//--> Cart
$objStrings 	= new Strings();

/*----- check session -----*/
if($ins_session->exist()) {
	$location = './myaccount.php';
	header("Location: $location");
				
	exit;

}

session_start();

$email		= $_GET['email'];
$password 	= $_GET['password'];

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);


if(isset($_POST['email'])){
	$email 		= $_POST['email'];
	$password 	= $_POST['password'];

	if($email == '')
		$error_message = "Email is required.";	//-- login name was empty
	else if($password == '')
		$error_message = "Error: No match for E-Mail Address and/or Password.";	//-- login password was empty

	if(empty($error_message)){
		$arrCustomers = $common->getRow(DB_TAG_PUBLIC, "SELECT id, password, lastname FROM customers WHERE email='$email'");
		
		if(!is_array($arrCustomers) || count($arrCustomers) < 1){
			$error_message = $email . " not existed.";
		}else if(!($objPassword->validate($password, $arrCustomers['password']))){
			$error_message = "Error: No match for E-Mail Address and/or Password.";
		}else {
			$arrdata = array();
			$arrdata['uname'] = $arrCustomers['lastname'];		//--> add login name to session
		
			if(is_array($ins_session->start($arrCustomers['id'], $arrdata))){
				if(isset($_POST['action']) && $_POST['action'] =="guest_email")
					$location = './checkout.php';
				else
					$location = './myaccount.php';
				
				
				header("Location: $location");
				
				exit;
			}
		}
	}
}

if(isset($_POST['action']) && $_POST['action'] =="guest_email" && !empty($error_message)){
	$location = "./preCheckout.php?error_message_login=$error_message";
				
	header("Location: $location");
	exit;		
}


include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->force_compile 	= true;
$smarty->debugging 		= false;
$smarty->caching 		= false;
$smarty->cache_lifetime = 120;

$smarty->assign('error_message', $error_message);

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('categorys', $categorys);
$smarty->assign('topCategory', $topCategory);
	
$smarty->assign('specProducts', $specProducts);

$smarty->assign('email', $email);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->display('login.html');
?>
